//
//  ZCMVClassifyViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCMVClassifyViewController.h"
#import "ZCAFNetWork.h"
#import "ZCMVModel.h"
#import <Masonry.h>
#import <MJRefresh.h>
#import "ZCMVUpdataCollectionViewCell.h"
#import "ZCMVAllClassifyViewController.h"
#import "ZCMVPlayerViewController.h"
@interface ZCMVClassifyViewController ()
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, copy) NSString *str;
@end

@implementation ZCMVClassifyViewController

- (void)dealloc
{
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor clearColor]];
    _dataArray = [[NSMutableArray alloc] init];
    self.count = 2;
    [self electricnetwork];
    [self creatVoew];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chuanzhi:) name:@"name" object:nil];
    
    // Do any additional setup after loading the view.
    
}

-(void)chuanzhi:(NSNotification *)chuanzhi {
    self.str = chuanzhi.userInfo[@"m"];
    [self electricnetwork];
}
#pragma mark 全部解析
- (void)electricnetwork
{
    if (self.str == nil) {
        self.str = @"0";
    }
    [_dataArray removeAllObjects];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/%@?size=12&page=1&order=2", self.str];
        [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
            if (result == nil) {
                return;
            }else {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                ZCMVModel *mvModel = [[ZCMVModel alloc] init];
                [mvModel setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:mvModel];
                [self.collectionView reloadData];
                //下拉刷新
                self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    [self loadNewData];
                }];
                //上拉加载
                self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    [self loadMoreData];
                }];
                [self.collectionView reloadData];
            }
            }
        } failure:^(NSError *error) {
            
    }];
}

#pragma mark 创建视图
- (void)creatVoew
{
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 5;

    _layout.itemSize = CGSizeMake(165 / 375.0 * self.view.frame.size.width, 130 / 667.0 * self.view.frame.size.height);
    //竖向之间的距离
    _layout.minimumLineSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 160) collectionViewLayout:_layout];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];
    self.collectionView.emptyDataSetDelegate = self;
    self.collectionView.emptyDataSetSource = self;
    [self.collectionView registerClass:[ZCMVUpdataCollectionViewCell class] forCellWithReuseIdentifier:@"d"];
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    self.collectionView.contentInset = UIEdgeInsetsMake(5, 10, 10, 10);
    [self.view addSubview:self.collectionView];
    // 最热最新按钮
    self.hotButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.hotButton.frame = CGRectMake(WIDTH - 120 , 0, 60 / 375.0 * WIDTH, 40 / 667.0 * HEIGHT);
    [self.view addSubview:self.hotButton];
    [self.hotButton setTitle:@"最热" forState:UIControlStateNormal];
    [self.hotButton addTarget:self action:@selector(hotClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.zNewButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.zNewButton.frame = CGRectMake(WIDTH - 60 , 0, 60 / 375.0 * WIDTH, 40 / 667.0 * HEIGHT);
    [self.view addSubview:self.zNewButton];
    [self.zNewButton setTitle:@"最新" forState:UIControlStateNormal];
    [self.zNewButton addTarget:self action:@selector(newlyClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 全部按钮
    self.allButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.allButton.frame = CGRectMake(10, 0, 60 / 375.0 * WIDTH, 40 / 667.0 * HEIGHT);
    [self.view addSubview:self.allButton];
    [self.allButton setTitle:@"全部" forState:UIControlStateNormal];
    [self.allButton addTarget:self action:@selector(allClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 最热
- (void)hotClick:(UIButton *)button
{
    [_dataArray removeAllObjects];
     NSString *str = @"http://api.dongting.com/song/video/type/0?size=12&page=1&order=2";
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.collectionView reloadData];
            //下拉刷新
            self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self loadNewDataHot];
            }];
            //上拉加载
            self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self loadMoreDataHot];
            }];
            [self.collectionView reloadData];
        }
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark 最新
- (void)newlyClick:(UIButton *)button
{
    [_dataArray removeAllObjects];
    [ZCAFNetWork getUrl:@"http://api.dongting.com/song/video/type/0?size=12&page=1&order=1" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.collectionView reloadData];
            //下拉刷新
            self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self loadNewDataNew];
            }];
            //上拉加载
            self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self loadMoreDataNew];
            }];
            [self.collectionView reloadData];
        }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)allClick:(UIButton *)button
{
    ZCMVAllClassifyViewController *all = [[ZCMVAllClassifyViewController alloc] init];
    [self.navigationController pushViewController:all animated:YES];
}

#pragma mark collection协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_dataArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZCMVUpdataCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"d" forIndexPath:indexPath];
    if (_dataArray.count > 1) {
        ZCMVModel * model = [_dataArray objectAtIndex:indexPath.row];
        [cell getInfo:model];
    }
    return cell;
    
}

#pragma mark 全部刷新加载
- (void)loadNewData
{
    if (self.str == nil) {
        self.str = @"0";
    }
    [_dataArray removeAllObjects];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/%@?size=12&page=1&order=2", self.str];
        [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
            if (result == nil) {
                return;
            }else {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                ZCMVModel *mvModel = [[ZCMVModel alloc] init];
                [mvModel setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:mvModel];
                [self.collectionView reloadData];
                [self.collectionView.header endRefreshing];
        }
            }
    } failure:^(NSError *error) {
        
    }];
}

- (void)loadMoreData
{
    if (self.str == nil) {
        self.str = @"0";
    }
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/%@?size=12&page=%ld&order=2", self.str, (long)self.count];
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.collectionView reloadData];
            [self.collectionView.header endRefreshing];
        }
        self.count = self.count + 1;
        [self.collectionView.footer endRefreshing];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark最热刷新加载
- (void)loadMoreDataNew
{

    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/0?size=12&page=%ld&order=1", (long)self.count];
        [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
            if (result == nil) {
                return;
            }else {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                ZCMVModel *mvModel = [[ZCMVModel alloc] init];
                [mvModel setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:mvModel];
                [self.collectionView reloadData];
            }
            self.count = self.count + 1;
            [self.collectionView.footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
        }];
    
}

- (void)loadNewDataNew
{
    [_dataArray removeAllObjects];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/0?size=12&page=1&order=1"];
        [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
            if (result == nil) {
                return;
            }else {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                ZCMVModel *mvModel = [[ZCMVModel alloc] init];
                [mvModel setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:mvModel];
                [self.collectionView reloadData];
                [self.collectionView.header endRefreshing];
            }
            }
        } failure:^(NSError *error) {
            
        }];
}

#pragma mark最新刷新加载
- (void)loadNewDataHot
{
    [_dataArray removeAllObjects];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/0?size=12&page=1&order=2"];
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.collectionView reloadData];
            [self.collectionView.header endRefreshing];
        }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)loadMoreDataHot
{
    
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/video/type/0?size=12&page=%ld&order=2", (long)self.count];
    [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCMVModel *mvModel = [[ZCMVModel alloc] init];
            [mvModel setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:mvModel];
            [self.collectionView reloadData];
        }
        self.count = self.count + 1;
        [self.collectionView.footer endRefreshing];
        }
    } failure:^(NSError *error) {
        
    }];
    
}
#pragma mark 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZCMVPlayerViewController *MVPlayVC =[[ZCMVPlayerViewController alloc] init];
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    UIView *view= (UIView *) [window.rootViewController.view viewWithTag:9999];
    view.hidden = YES;
    
    ZCMVModel * model = self.dataArray[indexPath.row];
    MVPlayVC.MVurl = model.mvList[0][@"url"];
    MVPlayVC.myId = model.uId;
    MVPlayVC.singerName = model.singerName;
    MVPlayVC.videoName = model.videoName;
    MVPlayVC.introduce = model.desc;
    [self.navigationController pushViewController:MVPlayVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
